package com.cxg.fairy.ec.main.search;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.ui.recycler.DataConverter;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;

import java.util.ArrayList;

/**
 * Created by CFairy
 */

public class SearchListDataConverter extends DataConverter {

    @Override
    public ArrayList<MultipleItemEntity> convert() {

        final ArrayList<MultipleItemEntity> dataList = new ArrayList<>();
        final JSONArray dataArray = JSON.parseObject(getJsonData()).getJSONArray("data");
        int total = JSON.parseObject(getJsonData()).getInteger("total");
        if (total == 0) {
            return null;
        } else {
        final int size = dataArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject data = dataArray.getJSONObject(i);
            final String thumb = data.getString("thumb");
            final String itemType = data.getString("itemType");
            final String price = data.getString("price");
            final int id = data.getInteger("skuId")==null?0:data.getInteger("skuId");
            final int spanSize = data.getInteger("spanSize");
            final String cartGoodsName = data.getString("goodsName");
            final String unit = data.getString("unit");

            int type = 0;
            if ("INDEXGOODS".equalsIgnoreCase(itemType)) {
                type = SearchListItemType.SEARCH_LIST_MIGHT_LIKE_GOODS;
            }

            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setField(MultipleFields.ITEM_TYPE, type)
                    .setField(MultipleFields.ID, id)
                    .setField(MultipleFields.IMAGE_URL, thumb)
                    .setField(SearchListItemFields.PRICE, price)
                    .setField(SearchListItemFields.GOODS_NAME, cartGoodsName)
                    .setField(SearchListItemFields.UNIT, unit)
                    .setField(SearchListItemFields.POSITION, i)
                    .setField(SearchListItemFields.SPAN_SIZE,spanSize)
                    .build();

            dataList.add(entity);
        }
        return dataList;
        }
    }

    @Override
    public MultipleItemEntity convertHeader() {
        return null;
    }
}
